package com.dp.temp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dp.util.AesGcm256Util;
import com.dp.util.HttpTool;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@RestController
@RequestMapping("/log")
public class sn_get_log {


    @PostMapping("/getSnLog")
    public void getSnLog( @RequestBody Map<String, Object> id){
        Map<String, String[]> map = new HashMap<>();
        String[] ids = id.get("id").toString().split(",");
        map.put("msgNos",ids);

        String jsonObject = JSONObject.toJSONString(map);
        System.out.println(jsonObject);
        HttpTool.http("http://safeaccess.sinochem.com/fuqmuqa6Zryu/common/v2/receive/get_log", jsonObject);
    }



    @PostMapping("/getSnLogErr")
    public void getSnLogErr( @RequestBody Map<String, Object> id){
        Map<String, String[]> map = new HashMap<>();
        String[] ids = id.get("id").toString().split(",");
        map.put("errorIds",ids);

        String jsonObject = JSONObject.toJSONString(map);
        System.out.println(jsonObject);
        HttpTool.http("http://safeaccess.sinochem.com/fuqmuqa6Zryu/common/v2/receive/get_request_log", jsonObject);
    }

    public static void http(@RequestParam String url, @RequestBody String object){
        try{
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            String data = AesGcm256Util.encrypt(object);
            JSONObject jsonObjecti = new JSONObject();
            jsonObjecti.put("data",data);
//            System.out.println("加密后的参数++++++++++" + data);
            String jsonString = JSON.toJSONString(jsonObjecti);
            StringEntity entity = new StringEntity(jsonString, "UTF-8");
            // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            httpPost.setHeader("Credit-Code","91140000717850635P");
            // 响应模型
            CloseableHttpResponse response = null;
            try {
                // 由客户端执行(发送)Post请求
                response = httpClient.execute(httpPost);
                // 从响应模型中获取响应实体
                HttpEntity responseEntity = response.getEntity();
                System.out.println("响应状态为:" + response.getStatusLine());
                if (responseEntity != null) {
                    System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                    String s = EntityUtils.toString(responseEntity);
                    System.out.println("响应内容为:" +s);
                    //解析json
//                    System.out.println(s);
                    JSONObject jsonObject=JSONObject.parseObject(s);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 释放资源
                    if (httpClient != null) {
                        httpClient.close();
                    }
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();

        }
    }



}
